package com.zyn.rsgl.controller;

import com.zyn.rsgl.pojo.*;
import com.zyn.rsgl.service.ScheduleService;
import com.zyn.rsgl.service.SignService;
import com.zyn.rsgl.utils.commonResult.R;
import org.springframework.http.MediaType;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.chrono.ChronoLocalDateTime;
import java.util.List;

/**
 * <p>
 * 打卡表 前端控制器
 * </p>
 *
 * @author zcs
 * @since 2025-02-24
 */
@RestController
@RequestMapping("/rsgl/sign")
public class SignController {

    @Resource
    private SignService signService;

    @Resource
    private ScheduleService scheduleService;

    // 查询所有
    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer pageNum,
                  @RequestParam(defaultValue = "10") Integer pageSize,
                  @RequestParam(required = false) String userId,
                  @RequestParam(required = false) LocalDateTime date,
                  @RequestParam(required = false) String punchType,
                  @RequestParam(required = false) String origin) {
        return signService.listPage(pageNum, pageSize, userId, date, punchType, origin);
    }

    // 添加打卡信息
    @PostMapping("/add")
    public R add(@RequestBody Sign sign) {
        // 检查是否已经打卡
        boolean exists = signService.exists(sign.getUserId(), sign.getDate(), sign.getPunchType());
        if (exists) {
            return R.fail("打卡已存在");
        }

        //检查当天有没有排班信息
        ScheduleDetail scheduleDetail = scheduleService.getScheduleDetailByDate(sign.getUserId(), sign.getDate());
        if (scheduleDetail == null || scheduleDetail.getIsWorkday().equals('0')) {
            return R.fail("当天没有排班信息");
        }

        // 检查是否迟到或早退
        if (sign.getPunchType().equals("上班卡")) {
            // 上班打卡
            LocalDateTime startDateTime = LocalDateTime.of(LocalDate.now(), scheduleDetail.getStartTime());
            if (sign.getDate().isAfter(startDateTime)) {
                return R.fail("迟到，无法打卡");
            }
        } else if (sign.getPunchType().equals("下班卡")) {
            // 下班打卡
            LocalDateTime endDateTime = LocalDateTime.of(LocalDate.now(), scheduleDetail.getEndTime());
            if (sign.getDate().isBefore(ChronoLocalDateTime.from(endDateTime))) {
                return R.fail("早退，无法打卡");
            }
        }

        boolean isSuccess = signService.save(sign);
        if (isSuccess) {
            return R.ok("打卡成功");
        }
        return R.fail("打卡失败");
    }

    // 获取每月打卡统计
    @GetMapping("/statistics")
    public R getMonthlyStatistics() {
        return signService.getMonthlyStatistics();
    }
    // 修改
    @PutMapping("/update")
    public R update(@RequestBody Sign sign) {
        return signService.updateById(sign) ? R.ok() : R.fail("修改失败");
    }

    // 删除打卡信息
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id) {
        return signService.removeById(id) ? R.ok() : R.fail("删除失败");
    }

}
